//
//  ZRCellPic.swift
//  Sina
//
//  Created by 张锐 on 16/3/2.
//  Copyright © 2016年 zhangr. All rights reserved.
//

import UIKit

/// item间距
let itemMargin : CGFloat = 5

/// item宽高
let itemWH : CGFloat = (UIScreen.mainScreen().bounds.width - (6 * itemMargin))/3

  let PicCollectionCellReuserId = "PicCollectionCellReuserId"

class ZRCellPic: UICollectionView ,UICollectionViewDataSource{
    

    
    var flowlayout : UICollectionViewFlowLayout = UICollectionViewFlowLayout()

    var picViewModel : ZRHomeCellViewModel?{
    
        didSet{
            self.snp_updateConstraints { (make) -> Void in
                
                make.size.equalTo(getSizeWithPictureNumbers())
            }
            
            reloadData()
        }
    }

//重写构造方法
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        super.init(frame: frame, collectionViewLayout: flowlayout)
        
        backgroundColor = UIColor.whiteColor()
        
        self.dataSource = self
        
        registerClass(ZRPictureCollectionCell.self, forCellWithReuseIdentifier: PicCollectionCellReuserId)
        
        flowlayout.itemSize = CGSizeMake(itemWH, itemWH)
        flowlayout.minimumInteritemSpacing = itemMargin
        flowlayout.minimumLineSpacing = itemMargin
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /**
     通过模型中图片数组图片数量来确定这个view多大
     */
    func getSizeWithPictureNumbers()->CGSize{
    
 
 
        //4特殊
        if let pic_count = picViewModel?.statusModel?.pic_urls?.count {
            if  pic_count == 4{
         
            return CGSizeMake( itemMargin + 2 * itemWH,  itemMargin + 2 * itemWH)
            
        }else{
            
            let rows : CGFloat = CGFloat((pic_count-1)/3+1)
                
                return CGSizeMake(2 * itemMargin + 3 * itemWH, rows * itemWH + (rows - 1)*itemMargin)
            
            
            }
        }
    
        return CGSizeMake(0, 0)
    
    }
   
}
// MARK: - collection 数据源方法
extension ZRCellPic{
    

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
    
    return picViewModel?.statusModel?.pic_urls?.count ?? 0
    
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
    
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(PicCollectionCellReuserId, forIndexPath: indexPath) as!ZRPictureCollectionCell
        
        let PicViewModel = ZRHomePicViewModel()
        
        PicViewModel.picModel = picViewModel?.statusModel?.pic_urls![indexPath.item]
        
        cell.picViewModel = PicViewModel
        
        return cell
    
    
    }


}
/// 自定义item
class ZRPictureCollectionCell : UICollectionViewCell {
    
    
    var picViewModel : ZRHomePicViewModel?{
    
    
        didSet{
        
        PicImageView.sd_setImageWithURL(picViewModel?.getUrlByPicModel())
        
        
        }
    
    
    }
    
    /**
    *  重写构造方法
    */
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    /**
    *  私有方法添加控件设置约束
    */
    private func setupUI(){
    
        addSubview(PicImageView)
    
    
        PicImageView.frame = self.bounds
    }
    /**
    *  懒加载控件
    */
    private lazy var PicImageView : UIImageView = {
    
        let picImageView = UIImageView()
        
        
        return picImageView
    
    }()
}
